"""
/code/System_Out.py\n
Wordcraft:System.Out\n
输出操作\n
"""

import colorama
from typing import List, Tuple


# class LogType(Enum):
#     """
#     Wordcraft:ClassDef.Basic.LogType\n
#     日志类型枚举\n
#     说明：
#     1. 日志类型枚举，用于输出日志的类型
#     2. 这种事就别麻烦隔壁的ClassDef了
#     """
#     Info = 0
#     Warning = 1
#     Error = 2


class Log:
    """
    Wordcraft:System.Out.Log\n
    """
    @staticmethod
    def Info(msg, **tofile):
        """
        Wordcraft:System.Out.Log.Info\n
        输出信息\n
        说明：\n
        1. 输出信息加上前缀[Info]\n
        2. 输出信息不带颜色\n
        """
        import json
        import time

        if msg != "":
            print("[Info]", msg)
        if tofile != {}:
            with open("gameconfig.json", "r") as f:
                world_name = json.load(f)["world_name"]
            with open("data/" + world_name + "/log.txt", "a", encoding="UTF-8") as f:
                f.write("[In World "+world_name+", at "+str(time.time())+"]\n")
                tofile.update({"type": "Info"})
                f.write(str(tofile).replace("'", '"').replace(
                    "True", "true").replace("False", "false")+"\n")

    @ staticmethod
    def Warning(msg, **tofile):
        """
        Wordcraft:System.Out.Log.Warning\n
        输出警告\n
        说明：\n
        1. 输出警告加上前缀[Warning]\n
        2. 输出警告不带颜色，前缀颜色为黄色\n
        """
        import json
        import time
        if msg != "":
            print(colorama.Fore.YELLOW + "[Warning]"+colorama.Fore.RESET, msg)
        if tofile != {}:
            with open("gameconfig.json", "r") as f:
                world_name = json.load(f)["world_name"]
            with open("data/" + world_name + "/log.txt", "a", encoding="UTF-8") as f:
                f.write("[In World "+world_name+", at "+str(time.time())+"]\n")
                tofile.update({"type": "Warning"})
                f.write(str(tofile).replace("'", '"').replace(
                    "True", "true").replace("False", "false")+"\n")

    @ staticmethod
    def Error(msg, **tofile):
        """
        Wordcraft:System.Out.Log.Error\n
        输出错误\n
        说明：\n
        1. 输出错误加上前缀[Error]\n
        2. 输出错误文字和前缀都是红色\n
        """
        import json
        import time
        if msg != "":
            print(colorama.Fore.RED + "[Error]", msg, colorama.Fore.RESET)
        if tofile != {}:
            with open("gameconfig.json", "r", encoding="UTF-8") as f:
                world_name = json.load(f)["world_name"]
            with open("data/" + world_name + "/log.txt", "a", encoding="UTF-8") as f:
                f.write("[In World "+world_name+", at "+str(time.time())+"]\n")
                tofile.update({"type": "Error"})
                f.write(str(tofile).replace("'", '"').replace(
                    "True", "true").replace("False", "false")+"\n")


def ShowText(obj, text: str, pos: Tuple, color, bgc, size, auto_update: bool = False):
    """
    Wordcraft:System.Out.ShowText\n
    在Pygame窗口显示文字\n
    参数：\n
    1. obj: Pygame窗口对象\n
    2. text: 文字\n
    3. pos: 文字位置\n
    4. color: 文字颜色\n
    5. bgc: 背景颜色\n
    6. size: 文字大小\n
    7. auto_update: 是否自动更新\n
    """
    import pygame
    import json

    pygame.init()
    obj: pygame.surface.Surface = obj  # 启用VScode的自动提示[doge]

    with open("gameconfig.json", "r") as f:
        font_path = json.load(f)["font"]  # 从配置文件读取字体文件路径

    font = pygame.font.Font(font_path, size)  # 加载字体
    text_surf = font.render(text, True, color, bgc)  # 生成文字
    obj.blit(text_surf, pos)  # 在窗口上显示文字
    if auto_update:
        pygame.display.update()  # 自动更新窗口


def ShowImage(obj, src: str, pos: Tuple, auto_update: bool = False):
    """
    Wordcraft:System.Out.ShowImage\n
    在Pygame窗口显示图片\n
    参数：\n
    1. obj: Pygame窗口对象\n
    2. src: 图片路径\n
    3. pos: 图片位置\n
    4. auto_update: 是否自动更新\n
    """
    import pygame

    pygame.init()
    obj: pygame.surface.Surface = obj  # 启用VScode的自动提示[doge]
    image_surf = pygame.image.load(src)  # 加载图片
    obj.blit(image_surf, pos)  # 在窗口上显示图片
    if auto_update:
        pygame.display.update()  # 自动更新窗口


def ShowBlock(obj, text, pos: Tuple, color, bgc, size, auto_update: bool = False):
    """
    Wordcraft:System.Out.ShowBlock\n
    在Pygame窗口显示排版为方块的文字\n
    参数：\n
    1. obj: Pygame窗口对象\n
    2. text: 文字\n
    3. pos: 文字位置\n
    4. color: 文字颜色\n
    5. bgc: 背景颜色\n
    6. size: 文字大小\n
    7. auto_update: 是否自动更新\n
    """
    import pygame

    obj: pygame.surface.Surface = obj  # 启用VScode的自动提示[doge]
    length = len(text)
    if length == 1:  # 单个字符
        """
        显示为
        一
        """
        ShowText(obj, text, pos, color, bgc, size, False)
    elif length == 2:  # 两个字符
        """
        显示为
        一〇
        〇二
        """
        ShowText(obj, text[0], pos, color, bgc, size//2, False)
        ShowText(obj, text[1], (pos[0]+size//2, pos[1] +
                 size//2), color, bgc, size//2, False)
    elif length == 3:  # 三个字符
        """
        显示为
        一〇
        二三
        """
        ShowText(obj, text[0], pos, color, bgc, size//2, False)
        ShowText(obj, text[1:], (pos[0], pos[1] + size//2),
                 color, bgc, size//2, False)
    elif length == 4:  # 四个字符
        """
        显示为
        一二
        三四
        """
        ShowText(obj, text[:2], pos, color, bgc, size//2, False)
        ShowText(obj, text[2:], (pos[0], pos[1] +
                                 size//2), color, bgc, size//2, False)
    else:  # 多于四个字符or少于一个字符
        """
        显示为
        材质
        丢失
        """
        text = "材质丢失"
        ShowText(obj, text[:2], pos, color, bgc, size//2, False)
        ShowText(obj, text[2:], (pos[0], pos[1] +
                                 size//2), color, bgc, size//2, False)

    if auto_update:
        pygame.display.update()  # 自动更新窗口


def LangText(name: str):
    """
    Wordcraft:System.Out.LangText\n
    获取语言库中的文本\n
    参数：\n
    1. name: 文本名称\n
    """
    import json

    with open("gameconfig.json", "r", encoding="UTF-8") as f:
        langname = json.load(f)["language"]  # 从配置文件读取语言库名称

    filename = "Source/Languages/" + langname + ".json"  # 语言库文件路径
    with open(filename, "r", encoding="UTF-8") as f:
        lang = json.load(f)
        return lang[name]  # 返回语言库中的文本


def ShowWorld(obj, x_spoce: Tuple, y_space: Tuple, auto_update: bool = True, *Chunks):
    """
    Wordcraft:System.Out.ShowWorld\n
    在Pygame窗口显示世界\n
    参数：\n
    1. obj: Pygame窗口对象\n
    2. x_spoce: X轴范围\n
    3. y_space: Y轴范围\n
    4. auto_update: 是否自动更新\n
    5. *Chunks: 三个区块\n
    """
    import pygame
    from Code.ClassDef_Else import Chunk
    from copy import deepcopy as dcp  # 使用深拷贝避免直接修改Chunks中的值

    obj: pygame.surface.Surface = obj  # 启用VScode的自动提示[doge]
    world: List[Chunk] = dcp(Chunks)  # 使用深拷贝避免直接修改Chunks中的值

    class Ranger:
        def __init__(self, screen_pos, map_pos):
            self.screen_pos = screen_pos
            self.map_pos = map_pos

    xRanger = Ranger((x_spoce[0], x_spoce[1]), (0, 0))
    yRanger = Ranger((y_space[1], y_space[1]), (0, 0))
